/*
 *
 * *******************************************************************
 *   @项目名称: BHex Android
 *   @文件名称: AssetApi.java
 *   @Date: 11/29/18 3:21 PM
 *   @Author: chenjun
 *   @Copyright（C）: 2018 BlueHelix Inc.   All rights reserved.
 *   注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的.
 *  *******************************************************************
 *
 */

package io.bhex.sdk.trade;

import android.text.TextUtils;

import java.util.List;

import io.bhex.baselib.network.Utils.CodeUtils;
import io.bhex.sdk.account.UserInfo;
import io.bhex.sdk.data_manager.SymbolDataManager;
import io.bhex.sdk.quote.bean.QuoteTokensBean;
import io.bhex.sdk.trade.bean.AddWithdrawAddressRequest;
import io.bhex.sdk.trade.bean.AddressCheckResponse;
import io.bhex.sdk.trade.bean.AddressListResponse;
import io.bhex.sdk.trade.bean.AllAssetResponse;
import io.bhex.sdk.trade.bean.AssetDataResponse;
import io.bhex.sdk.trade.bean.AssetRecordResponse;
import io.bhex.sdk.trade.bean.CodeOrderIdBean;
import io.bhex.sdk.trade.bean.CoinAddressBean;
import io.bhex.sdk.trade.bean.FeeBeanResponse;
import io.bhex.sdk.trade.bean.FuturesAssetListResponse;
import io.bhex.sdk.trade.bean.OptionAssetListResponse;
import io.bhex.sdk.trade.bean.RecordBeanResponse;
import io.bhex.sdk.trade.bean.TransferAssetRequest;
import io.bhex.sdk.trade.bean.TwoVerifyBean;
import io.bhex.sdk.trade.bean.VerifyWithDrawRequest;
import io.bhex.sdk.trade.bean.WithDrawRequstBean;
import io.bhex.sdk.trade.bean.WithDrawVerifyBean;
import io.bhex.sdk.trade.bean.WithdrawCreateRequest;
import io.bhex.sdk.Urls;
import io.bhex.baselib.constant.AppData;
import io.bhex.baselib.constant.Fields;
import io.bhex.baselib.network.BParamsBuilder;
import io.bhex.baselib.network.HttpUtils;
import io.bhex.baselib.network.listener.SimpleResponseListener;
import io.bhex.baselib.network.params.GetParams;
import io.bhex.baselib.network.params.PostParams;
import io.bhex.baselib.network.response.ResponseListener;
import io.bhex.baselib.network.response.ResultResponse;
import io.bhex.sdk.trade.futures.bean.TradableBeanResponse;

public class AssetApi {
    /**
     * 添加提币地址
     * @param requestData
     * @param listener
     */
    public static void RequestAddWithdrawAddress(AddWithdrawAddressRequest requestData, ResponseListener<ResultResponse> listener) {

        if(requestData == null || requestData.verifyBean == null)
            return;
        PostParams.Builder build = BParamsBuilder.post()
                .url(Urls.API_ASSET_B_WITHDRAW_ADDRESS_ADD)
                .addParam("token_id",requestData.tokenId)
                .addParam("address",requestData.address)
                .addParam("remark",requestData.addressRemark)
                .addParam("auth_type",requestData.verifyBean.getAuth_type())
                .addParam("order_id",requestData.verifyBean.getOrder_id())
                .addParam("verify_code",requestData.verifyBean.getVerify_code())
                .addParam("chain_type",requestData.chainType);

        if(!TextUtils.isEmpty(requestData.address_ext))
            build.addParam("address_ext",requestData.address_ext);

        HttpUtils.getInstance().request(build.build(), ResultResponse.class, listener);

    }

    /**
     * 提币常用地址
     *
     * @param tokenId
     * @param chainType
     * @param listener
     */
    public static void RequestAddressListByToken(String tokenId,String chainType, ResponseListener<AddressListResponse> listener) {
        PostParams params = BParamsBuilder.post()
                .url(Urls.API_ASSET_B_WITHDRAW_ADDRESS)
                .addParam("token_id",tokenId)
                .addParam("chain_type",chainType)
                .build();
        HttpUtils.getInstance().request(params, AddressListResponse.class, listener);
    }

    /**
     *RequestAllAddress
     * @param listener
     */
    public static void RequestAllAddress(ResponseListener<AddressListResponse> listener) {
        PostParams params = BParamsBuilder.post()
                .url(Urls.API_ASSET_B_WITHDRAW_ADDRESS)
                .build();
        HttpUtils.getInstance().request(params, AddressListResponse.class, listener);
    }


    /**
     *RequestQuoteTokens
     * @param listener
     */
    public static void RequestQuoteTokens(final ResponseListener<List<QuoteTokensBean.TokenItem>> listener) {

        if(SymbolDataManager.GetInstance().getTokenList() != null && listener != null){
            listener.onSuccess(SymbolDataManager.GetInstance().getTokenList());
            return;
        }
        GetParams params = BParamsBuilder.get()
                .url(Urls.API_TOKEN_LIST_URL)
                .build();
        HttpUtils.getInstance().request(params, QuoteTokensBean.class, new SimpleResponseListener<QuoteTokensBean>() {

            @Override
            public void onSuccess(QuoteTokensBean response) {
                super.onSuccess(response);
                if (CodeUtils.isSuccess(response, true)) {
                    if(listener != null)
                        listener.onSuccess(response.getArray());
                }
            }


        });
    }



    /**
     * RequestTokenIdAsset
     * @param tokenId
     * @param listener
     */
    public static void RequestTokenIdAsset(String tokenId, SimpleResponseListener<AssetDataResponse> listener){
        PostParams params = BParamsBuilder.post()
                .url(Urls.API_ASSET_GET_LIST)
                .addParam("account_id", UserInfo.getDefaultAccountId())
                .addParam("token_ids", tokenId)
                .build();
        HttpUtils.getInstance().request(params,AssetDataResponse.class, listener);
    }

    public static void RequestOptionTokenIdAsset(String tokenId, SimpleResponseListener<OptionAssetListResponse> listener){
        PostParams params = BParamsBuilder.post()
                .url(Urls.API_ACCOUNT_GET_OPTION_LIST)
                //.addParam("account_id", UserInfo.getDefaultAccountId())
                .addParam("token_ids", tokenId)
                .build();
        HttpUtils.getInstance().request(params,OptionAssetListResponse.class, listener);
    }

    /**
     * 获取 期货资产
     * @param tokenIds
     * @param listener
     */
    public static void RequestFuturesTokenIdAsset(String tokenIds, SimpleResponseListener<FuturesAssetListResponse> listener){
        PostParams params = BParamsBuilder.post()
                .url(Urls.API_FUTURES_ASSET_AVAILABLE)
                //.addParam("account_id", UserInfo.getDefaultAccountId())
                .addParam("token_ids", tokenIds)//多个token 逗号隔开
//                .addParam("filter_explore",1)    //是否过滤体验币， 1 = 过滤， 0 = 不过滤。默认=0
                .build();
        HttpUtils.getInstance().request(params,FuturesAssetListResponse.class, listener);
    }

    /**
     * 期货可交易信息
     */
    public static void getAssetTradable(String tokenIds, SimpleResponseListener<TradableBeanResponse> observer) {

        GetParams.Builder builder = new BParamsBuilder().get();
        builder.url(Urls.API_FUTURES_ASSET_TRADABLE)
                .addParam("token_ids", tokenIds);
        GetParams params = builder.build();
        HttpUtils.getInstance().request(params, TradableBeanResponse.class, observer);

    }

    /**
     * 删除地址
     * @param address_id
     * @param verifyBean
     * @param observer
     */
    @Deprecated
    public static void RequestDeleteAddress(String address_id, TwoVerifyBean verifyBean, ResponseListener<ResultResponse> observer) {
        if(verifyBean == null)
            return;
        PostParams params = BParamsBuilder.post()
                .url(Urls.API_ASSET_B_WITHDRAW_ADDRESS_DELETE)
                .addParam("address_id",address_id)
                .addParam("auth_type",verifyBean.getAuth_type())
                .addParam("order_id",verifyBean.getOrder_id())
                .addParam("verify_code",verifyBean.getVerify_code())
                .build();
        HttpUtils.getInstance().request(params, ResultResponse.class, observer);
    }

    /**
     * 删除地址
     * @param address_id
     * @param observer
     */
    public static void RequestDeleteAddress(String address_id, ResponseListener<ResultResponse> observer) {
        PostParams params = BParamsBuilder.post()
                .url(Urls.API_ASSET_B_WITHDRAW_ADDRESS_DELETE)
                .addParam("address_id",address_id)
                .build();
        HttpUtils.getInstance().request(params, ResultResponse.class, observer);
    }

    /**
     * 检查提币地址
     * @param token_id
     * @param address
     * @param address_ext
     * @param observer
     */
    public static void checkAddress(String token_id,String chainType,String address,String address_ext,ResponseListener<AddressCheckResponse> observer) {
        PostParams params;
        if (TextUtils.isEmpty(address_ext)) {
            params = BParamsBuilder.post()
                    .url(Urls.API_ASSET_B_WITHDRAW_ADDRESS_CHECK)
                    .addParam("token_id",token_id)
                    .addParam("address",address.trim())
                    .addParam("chain_type",chainType)
                    .build();
        }else{
            params = BParamsBuilder.post()
                    .url(Urls.API_ASSET_B_WITHDRAW_ADDRESS_CHECK)
                    .addParam("token_id",token_id)
                    .addParam("address",address.trim())
                    .addParam("address_ext",address_ext.trim())
                    .addParam("chain_type",chainType)
                    .build();
        }

        HttpUtils.getInstance().request(params, AddressCheckResponse.class, observer);
    }

    /**
     *
     * @param tokenId
     * @param pageId
     * @param listener
     */
    public static void RequestAssetRecordHistory(String tokenId, String pageId, SimpleResponseListener<AssetRecordResponse> listener) {

        String url = Urls.API_ASSET_RECORD_HISTORY;

        PostParams params = BParamsBuilder.post()
                .url(url)
                .addParam(Fields.FIELD_ACCOUNT_ID, UserInfo.getDefaultAccountId())
                .addParam(Fields.FIELD_TOKEN_ID, tokenId)
                .addParam(Fields.FIELD_ORDER_ID_PAGE_FROM, pageId)
                .addParam(Fields.FIELD_LIMIT,AppData.Config.PAGE_LIMIT)
                .build();
        HttpUtils.getInstance().request(params, AssetRecordResponse.class, listener);
    }

    //获取记录
    /**
     *RequestAssetRecord
     * @param tabType 0 RECHARGE_HISTORY, 1 WITHDRAW_HISTOR, 2 OTHER_HISTORY
     */
    public static void RequestAssetRecord(int tabType, String tokenId, String pageId, SimpleResponseListener<RecordBeanResponse> listener) {
        String url = Urls.API_ASSET_B_OTHER_HISTORY;
        switch (tabType){
            case 0:
                //充币记录
                url = Urls.API_ASSET_B_RECHARGE_HISTORY;
                break;
            case 1:
                //提币记录
                url = Urls.API_ASSET_B_WITHDRAW_HISTORY;
                break;
            case 2:
                //其他
                url = Urls.API_ASSET_RECORD_HISTORY;
                break;
        }

        PostParams params = BParamsBuilder.post()
                .url(url)
                .addParam(Fields.FIELD_TOKEN_ID, tokenId)
                .addParam(Fields.FIELD_ORDER_ID_PAGE_FROM, pageId)
                .addParam(Fields.FIELD_ACCOUNT_ID, UserInfo.getDefaultAccountId())
                .addParam(Fields.FIELD_LIMIT,AppData.Config.PAGE_LIMIT)
                .build();
        HttpUtils.getInstance().request(params, RecordBeanResponse.class, listener);
    }

    /**
     * 获取充币地址
     */
    public static void RequestRechargeAddress(String token,String chainType, ResponseListener listener) {
        PostParams params = BParamsBuilder.post()
                .url(Urls.API_ASSET_B_RECHARGE_ADDRESS)
                .addParam(Fields.FIELD_ACCOUNT_ID, UserInfo.getDefaultAccountId())
                .addParam(Fields.FIELD_TOKEN_ID, token)
                .addParam(Fields.FIELD_CHAIN_TYPE, chainType)
                .build();
        HttpUtils.getInstance().request(params, CoinAddressBean.class, listener);
    }

    /**
     * 获取手续费
     *
     * @param token
     * @param chainType
     */
    public static void RequestQuotaInfo(String token, String chainType, SimpleResponseListener<FeeBeanResponse> listener) {
        PostParams params = BParamsBuilder.post()
                .url(Urls.API_ASSET_QUOTA_INFO)
                .addParam(Fields.FIELD_ACCOUNT_ID, UserInfo.getDefaultAccountId())
                .addParam(Fields.FIELD_TOKEN_ID, token)
                .addParam(Fields.FIELD_CHAIN_TYPE, chainType)
                .build();
        HttpUtils.getInstance().request(params, FeeBeanResponse.class, listener);
    }

    /**
     * 获取资产托管平台验证码
     * @param requestId
     */
    public static void RequstBrokerVerifyCode(String requestId, SimpleResponseListener<CodeOrderIdBean> listener) {
        PostParams params = BParamsBuilder.post()
                .url(Urls.API_ASSET_B_WITHDRAW_P_VERIFY)
                .addParam("request_id",requestId)
                .build();
        HttpUtils.getInstance().request(params,CodeOrderIdBean.class,listener);
    }

    /**
     * 发起提币请求
     */
    public static void RequstWithdrawCreate(WithdrawCreateRequest requestData, SimpleResponseListener<WithDrawRequstBean> listener) {
        PostParams params = BParamsBuilder.post()
                .url(Urls.API_ASSET_B_WITHDRAW_CREATE)
                .addParam(Fields.FIELD_ACCOUNT_ID, UserInfo.getDefaultAccountId())
                .addParam(Fields.FIELD_TOKEN_ID, requestData.token)
                .addParam(Fields.FIELD_CLIENT_ORDER_ID, System.currentTimeMillis())
                .addParam(Fields.FIELD_ADDRESS, requestData.address.trim())
                .addParam("address_id", requestData.addressId)
                .addParam(Fields.FIELD_QUANTITY, requestData.amount)
                .addParam("convert_rate", requestData.convertRate)
                .addParam("miner_fee", requestData.minerFee)
                .addParam("auth_type", requestData.authType)
                .addParam("address_ext", requestData.isNeedTag?requestData.tag:"")
                .addParam(Fields.FIELD_ORDER_ID, requestData.orderId)
                .addParam(Fields.FIELD_VERIFY_CODE, requestData.verifyCode)
                .addParam("trade_password", requestData.tradePasswd)
                .addParam("chain_type", requestData.chainType)
                .build();
        HttpUtils.getInstance().request(params, WithDrawRequstBean.class, listener);
    }

    /**
     * 验证请求
     * @param requestData
     * @param listener
     */
    public static void RequestVerifyWithdraw(VerifyWithDrawRequest requestData, SimpleResponseListener<WithDrawVerifyBean> listener) {
        if(requestData == null)
            return;
        PostParams params;
        if (requestData.isShowIdCardCheck) {
            params = BParamsBuilder.post()
                    .url(Urls.API_ASSET_B_WITHDRAW_VERIFY)
                    .addParam(Fields.FIELD_ACCOUNT_ID, UserInfo.getDefaultAccountId())
                    .addParam("token_id", requestData.tokenId)
                    .addParam("client_order_id", System.currentTimeMillis())
                    .addParam("request_id", requestData.requestId)
                    .addParam("code_order_id", requestData.currentCodeId)
                    .addParam("verify_code", requestData.code)
                    .addParam("skip_input_id_card_no", requestData.isSkip)
                    .addParam("id_card_no", requestData.idcard)
                    .build();
        }else{
            params = BParamsBuilder.post()
                    .url(Urls.API_ASSET_B_WITHDRAW_VERIFY)
                    .addParam(Fields.FIELD_ACCOUNT_ID, UserInfo.getDefaultAccountId())
                    .addParam("token_id", requestData.tokenId)
                    .addParam("client_order_id", System.currentTimeMillis())
                    .addParam("request_id", requestData.requestId)
                    .addParam("code_order_id", requestData.currentCodeId)
                    .addParam("verify_code", requestData.code)
                    .build();
        }

        HttpUtils.getInstance().request(params, WithDrawVerifyBean.class, listener);
    }

    /**
     * 取消提币
     */
    public static void RequstCancelWithdraw(String orderId,SimpleResponseListener<ResultResponse> listener) {
        PostParams params = BParamsBuilder.post()
                .url(Urls.API_ASSET_B_WITHDRAW_CANCEL)
                .addParam("order_id",orderId)
                .build();
        HttpUtils.getInstance().request(params,ResultResponse.class,listener);
    }

    /**
     * 全部资产
     */
    public static void RequstAllAsset(SimpleResponseListener<AllAssetResponse> listener) {
        PostParams params = BParamsBuilder.post()
                .url(Urls.API_ASSET_ALL)
                .addParam("unit",AppData.Config.LEGAL_MONEY_ABOUT_VALUE_CAL_UNIT)
                .build();
        HttpUtils.getInstance().request(params,AllAssetResponse.class,listener);
    }

    /**
     * 全部资产
     */
    public static void RequstTransferAsset(TransferAssetRequest requestData, ResponseListener<ResultResponse> observer) {
        if(requestData == null)
            return;
        PostParams params = BParamsBuilder.post()
                .url(Urls.API_ASSET_TRANSFER)
                .addParam("token_id", requestData.token_id)
                .addParam("quantity", requestData.quantity)
                .addParam("source_type", requestData.source_type)
                .addParam("target_type", requestData.target_type)
                .build();
        HttpUtils.getInstance().request(params,ResultResponse.class,observer);
    }

}
